<?php
require_once ("person_controller.php");
class Staffs extends Person_controller
{
	function __construct()
	{
		parent::__construct('staffs');
	}
	
	function index()
	{
		$data['controller_name']=strtolower($this->uri->segment(1));
		$data['form_width']=$this->get_form_width();
		$data['manage_table']=staffs_people_manage_table($this->Staff->get_all(),$this);
		$this->load->view('people/staff_manage',$data);
	}
	
	/*
	Returns Staff table data rows. This will be called with AJAX.
	*/
	function search()
	{
		$search=$this->input->post('search');
		$data_rows=staffs_people_manage_table_data_rows($this->Staff->search($search),$this);
		echo $data_rows;
	}
	
	/*
	Gets one row for a person manage table. This is called using AJAX to update one row.
	*/
	function get_row_staff()
	{
		$person_id = $this->input->post('row_id');
		$data_row=staffs_person_data_row($this->Staff->get_info($person_id),$this);
		echo $data_row;
	}
	
	/*
	Gives search suggestions based on what is being searched for
	*/
	function suggest()
	{
		$suggestions = $this->Staff->get_search_suggestions($this->input->post('q'),$this->input->post('limit'));
		echo implode("\n",$suggestions);
	}
	
	/*
	Loads the Staff edit form
	*/
	function view($staff_id=-1)
	{
		$data['person_info']=$this->Staff->get_info($staff_id);
		$banks = array('0' => $this->lang->line('items_none'));
		foreach($this->Bank->get_all()->result_array() as $row)
		{
			$banks[$row['bank_id']] = $row['name'] ;
		}
		$data['banks']=$banks;
		$data['selected_bank'] = $data['person_info']->bank_type;
		$this->load->view("staffs/form",$data);
	}
	
	
	/*
	Loads the Staff profile
	*/
	function view_profile($staff_id=-1)
	{
		$data['person_info']=$this->Staff->get_info($staff_id);
		$bank = $this->Bank->get_info($data['person_info']->bank_type)->name ; 	
		$data['bank']=$bank;
		$this->load->view("staffs/view_profile",$data);
	}
	
	/*
	Inserts/updates a staff
	*/
	function save($staff_id=-1)
	{
		$person_data = array(
		'first_name'=>$this->input->post('first_name'),
		'last_name'=>$this->input->post('last_name'),
		'email'=>$this->input->post('email'),
		'phone_number'=>$this->input->post('phone_number'),
		'address_1'=>$this->input->post('address_1'),
		'address_2'=>$this->input->post('address_2'),
		'city'=>$this->input->post('city'),
		'state'=>$this->input->post('state'),
		//'zip'=>$this->input->post('zip'),
		'zip'=>'',
		'country'=>$this->input->post('country'),
		'comments'=>$this->input->post('comments')
		);
		$staff_data=array(
		'account_number'=>$this->input->post('account_number')=='' ? null:$this->input->post('account_number'),
		'bank_type'=>$this->input->post('bank_type')=='' ? null:$this->input->post('bank_type'),
		'bank_account_number'=>$this->input->post('bank_account_number')=='' ? null:$this->input->post('bank_account_number'),
		'position'=>$this->input->post('position')=='' ? null:$this->input->post('position'),
		'social_security_no'=>$this->input->post('social_security_no')=='' ? null:$this->input->post('social_security_no')
		//'ss_contributor'=>$this->input->post('ss_contributor')=='' ? 0:1,
		//'taxable'=>$this->input->post('taxable')=='' ? 0:1
		);
		if($this->Staff->save($person_data,$staff_data,$staff_id))
		{
			//New staff
			if($staff_id==-1)
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('staffs_successful_adding').' '.
				$person_data['first_name'].' '.$person_data['last_name'],'person_id'=>$staff_data['person_id']));
			}
			else //previous staff
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('staffs_successful_updating').' '.
				$person_data['first_name'].' '.$person_data['last_name'],'person_id'=>$staff_id));
			}
		}
		else//failure
		{	
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('staffs_error_adding_updating').' '.
			$person_data['first_name'].' '.$person_data['last_name'],'person_id'=>-1));
		}
	}
	
	/*
	This deletes staffs from the staffs table
	*/
	function delete()
	{
		$staffs_to_delete=$this->input->post('ids');
		
		if($this->Staff->delete_list($staffs_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('staffs_successful_deleted').' '.
			count($staffs_to_delete).' '.$this->lang->line('staffs_one_or_multiple')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('staffs_cannot_be_deleted')));
		}
	}
	
	/*
	get the width for the add/edit form
	*/
	function get_form_width()
	{			
		return 360;
	}
	
	
	function detailed_payroll($payroll_id)
	{
		$data = array();
		$data = $this->get_payroll_info($payroll_id);
		$payroll_info = $this->Staff->get_payroll_for_editing($payroll_id);
		//Allowance
		$allowance = array();
		foreach($payroll_info['query_allowance']->result_array() as $row)
		{	
			$str = $row['taxable'] == '0' ? $row['name'].'&nbsp;&nbsp;&nbsp;&nbsp; ( not taxed )': $row['name'].'&nbsp;&nbsp;&nbsp;&nbsp; ( taxed )';
			$allowance[$str] = $row['amount'] ; 
		}
		//Deductions
		$deductions = array();
		foreach($payroll_info['query_deductions']->result_array() as $row)
		{
			$deductions[$row['name']] = $row['amount'] ; 	
		}
		
		$data['allowance']=$allowance;
		$data['deductions']=$deductions;
		$this->load->view("staffs/detailed_payroll", $data);
	}
	
	//To print a payroll for a month
	function print_payroll($payroll_id)
	{
		$data = array();
		$data = $this->get_payroll_info($payroll_id);
		$data['person_info']=$this->Staff->get_info($data['staff_id']);
		$payroll_info = $this->Staff->get_payroll_for_editing($payroll_id);
		//Allowance
		$allowance = array();
		foreach($payroll_info['query_allowance']->result_array() as $row)
		{
			$str = $row['taxable'] == '0' ? $row['name'].'&nbsp;&nbsp;&nbsp;&nbsp; ( not taxed )': $row['name'].'&nbsp;&nbsp;&nbsp;&nbsp; ( taxed )';
			$allowance[$str] = $row['amount'] ; 	
		}
		//Deductions
		$deductions = array();
		foreach($payroll_info['query_deductions']->result_array() as $row)
		{
			$deductions[$row['name']] = $row['amount'] ; 	
		}
		
		$data['allowance']=$allowance;
		$data['deductions']=$deductions;
		$data['controller']=strtolower($this->uri->segment(1));
		
		$this->load->view("staffs/print_payroll", $data);
	}
	
	
	function get_account($staff_id=-1)
	{
		$sortable_data = array();
		$staff_payrolls = $this->Staff->get_staff_payrolls($staff_id);
		foreach($staff_payrolls->result() as $row)
		{	
			$raw_data = array();
			
			$payroll_id = $row->payroll_id;
			$date = $row->date;
			$year = $row->year;
			$month = $row->month;
			$day = $row->day;
			$tax_cat = $row->tax_cat;
			$ssf_cat = $row->ssf_cat;
			$basic_wages = $row->basic_wages;
		
			//Get the total  allawance
			$allowance_result = $this->Staff->get_allowance_totals($payroll_id);
			if($allowance_result->num_rows() > 0)
			{
				$allowance_row = $allowance_result->row();
				$allowance_totals = $allowance_row->payroll_total;
				if(empty($allowance_totals))
				{
					$allowance_totals = 0;
				}
			}
			else
			{
				$allowance_totals = 0;
			}
			$allowance_totals = number_format($allowance_totals, 2, '.', '');
			
			//Get the total  non taxable allawance
			$no_tax_allowance_result = $this->Staff->get_non_taxable_allowance_totals($payroll_id);
			if($no_tax_allowance_result->num_rows() > 0)
			{
				$no_tax_allowance_row = $no_tax_allowance_result->row();
				$no_tax_allowance_totals = $no_tax_allowance_row->payroll_total;
				if(empty($no_tax_allowance_totals))
				{
					$no_tax_allowance_totals = 0;
				}
			}
			else
			{
				$no_tax_allowance_totals = 0;
			}
			$no_tax_allowance_totals = number_format($no_tax_allowance_totals, 2, '.', '');
			
			///////Get the total deductions//////
			$deductions_result = $this->Staff->get_deduction_totals($payroll_id);
			if($deductions_result->num_rows() > 0)
			{
				$deductions_row = $deductions_result->row();
				$deduction_totals = $deductions_row->payroll_total;
				if(empty($deduction_totals))
				{
					$deduction_totals = 0;
				}
			}
			else
			{
				$deduction_totals = 0;
			}
			$deduction_totals = number_format($deduction_totals, 2, '.', '');
			
			////////////*************************** Now Do Tax and SS and Others**************************** ////////////			
			
			$all_calc = $this->all_calc($basic_wages,$allowance_totals,$ssf_cat,$tax_cat,$deduction_totals,$no_tax_allowance_totals);
			
			/////////////////////////////////////////********************************************////////////////////////////////////
			
			$raw_data[] = $payroll_id;
			$raw_data[] = $date;
			$raw_data[] = $basic_wages;
			//$raw_data[] = $allowance_totals;
			
			/*if(is_array($ssf_array))
			{
				$raw_data[] = $ssf_array[0];
			}
			else
			{
				$raw_data[] = $ssf_array;
			}*/
			//$raw_data[] = $net_basic_wage;
			$raw_data[] = $all_calc['gross_pay'];
			
			
			//$raw_data[] = $tax;
			//$raw_data[] = $taxable_income;
			//$raw_data[] = $deduction_totals;
			$raw_data[] = $all_calc['net_pay'];
			//$raw_data[] = $total_payroll_deductions;
			$raw_data[] = $all_calc['total_ssf_payable'];
			
			$sortable_data[] = $raw_data;	
		}
		
		return $sortable_data;
	}
	
	
	//Get a detailed_payroll
	function get_payroll_info($payroll_id=-1)
	{		
		$payroll_info = $this->Staff->get_payroll_for_editing($payroll_id);
		$payroll = $payroll_info['query_payroll']->row();
		$date = $payroll->date;
		$year = $payroll->year;
		$month = $payroll->month;
		$day = $payroll->day;
		$tax_cat = $payroll->tax_cat;
		$ssf_cat = $payroll->ssf_cat;
		$basic_wages = $payroll->basic_wages;
		$staff_id = $payroll->person_id;
		
		//Get the total  allawance
		$allowance_result = $this->Staff->get_allowance_totals($payroll_id);
		if($allowance_result->num_rows() > 0)
		{
			$allowance_row = $allowance_result->row();
			$allowance_totals = $allowance_row->payroll_total;
			if(empty($allowance_totals))
			{
				$allowance_totals = 0;
			}
		}
		else
		{
			$allowance_totals = 0;
		}
		$allowance_totals = number_format($allowance_totals, 2, '.', '');
		
		//Get the total  non taxable allawance
		$no_tax_allowance_result = $this->Staff->get_non_taxable_allowance_totals($payroll_id);
		if($no_tax_allowance_result->num_rows() > 0)
		{
			$no_tax_allowance_row = $no_tax_allowance_result->row();
			$no_tax_allowance_totals = $no_tax_allowance_row->payroll_total;
			if(empty($no_tax_allowance_totals))
			{
				$no_tax_allowance_totals = 0;
			}
		}
		else
		{
			$no_tax_allowance_totals = 0;
		}
		$no_tax_allowance_totals = number_format($no_tax_allowance_totals, 2, '.', '');
		
			
			///////Get the total deductions//////
		$deductions_result = $this->Staff->get_deduction_totals($payroll_id);
		if($deductions_result->num_rows() > 0)
		{
			$deductions_row = $deductions_result->row();
			$deduction_totals = $deductions_row->payroll_total;
			if(empty($deduction_totals))
			{
				$deduction_totals = 0;
			}
		}
		else
		{
			$deduction_totals = 0;
		}
		$deduction_totals = number_format($deduction_totals, 2, '.', '');
		
		////////////*************************** Now Do Tax and SS and Others**************************** ////////////
		
			$all_calc = $this->all_calc($basic_wages,$allowance_totals,$ssf_cat,$tax_cat,$deduction_totals,$no_tax_allowance_totals);
			
			/////////////////////////////////////////********************************************////////////////////////////////////\
			$data['staff_id'] = $staff_id;
			$data['payroll_id'] = $payroll_id;
			$data['date'] = $date;
			$data['basic_wages'] = $basic_wages;
			$data['allowance_totals'] = $allowance_totals;
			
			$data['ssf_array'] = $all_calc['ssf_array'];
			
			$data['net_basic_wage'] = $all_calc['net_basic_wage'];
			$data['gross_pay'] = $all_calc['gross_pay'];
			$data['tax'] = $all_calc['tax'];
			$data['taxable_income'] = $all_calc['taxable_income'];
			$data['deduction_totals'] = $deduction_totals;
			$data['net_pay'] = $all_calc['net_pay'];
			$data['total_payroll_deductions'] = $all_calc['total_payroll_deductions'];
			$data['total_ssf_payable'] = $all_calc['total_ssf_payable'];
			$data['default_ssf_percent_value'] = $all_calc['default_ssf_percent_value'];
			$data['default_ssf_value'] = $all_calc['default_ssf_value'];
			$data['ssf_percent_value_pair'] = $all_calc['ssf_percent_value_pair'];
			
				
		return $data;
	}
	
	///////////////////CALCULATING FUNCTIONS///////////////////////////
	
	//CAll the calculating functions
	function all_calc($basic_wages,$allowance_totals,$ssf_cat,$tax_cat,$deduction_totals,$no_tax_allowance_totals)
	{
		$result = array();
		//Get the default ssf percent value
		$default_ssf_value = 			$this->cal_default_ssf($ssf_cat,$basic_wages);
		//Calc Gross Pay
		$gross_pay = 					$this->cal_gross_pay($basic_wages,$allowance_totals);
		//Calc Net Basic Wage
		$net_basic_wage = 				$this->cal_net_basic_wage($basic_wages,$default_ssf_value);
		//Calc SSF
		$ssf_array = 					$this->cal_ssf($ssf_cat,$basic_wages);
		//Taxable Income
		$taxable_income = 				$this->cal_taxable_income($gross_pay,$default_ssf_value) - $no_tax_allowance_totals;
		//Tax
		$tax = 							$this->cal_tax($tax_cat,$taxable_income);
		//Total Deductions
		$total_payroll_deductions = 	$this->cal_total_payroll_deductions($tax,$deduction_totals);
		//Net Pay
		$net_pay = 						$this->cal_net_pay($taxable_income,$total_payroll_deductions) + $no_tax_allowance_totals;
		//Total SSF Payable
		$total_ssf_payable = 			$this->cal_total_ssf_payable($ssf_array);
		//Get the default ssf percent value
		$default_ssf_percent_value = 	$this->default_ssf_percent_value($ssf_cat);
		//SSFs Percent and Value Pair
		$ssf_percent_value_pair = 		$this->cal_ssf_percent_value_pair($ssf_cat,$basic_wages);
		
		
		$result['gross_pay'] = 	$gross_pay;				
		$result['net_basic_wage'] = $net_basic_wage	;		
		$result['ssf_array'] = 	$ssf_array;			
		$result['taxable_income'] = $taxable_income	;			
		$result['tax'] = 	$tax;						
		$result['net_pay'] = 	$net_pay;					
		$result['total_payroll_deductions'] = 	$total_payroll_deductions;
		$result['total_ssf_payable'] = 	$total_ssf_payable;
		$result['default_ssf_percent_value'] = 	$default_ssf_percent_value;
		$result['default_ssf_value'] = 	$default_ssf_value;
		$result['ssf_percent_value_pair'] = 	$ssf_percent_value_pair;

		return $result;
	}
	
	//Calc SSF
	function cal_ssf($ssf_cat,$gross_pay)
	{			
		//SSFs
		if(is_numeric($ssf_cat))
		{
			//Get ssf Components
			$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
			if($ssf_items->num_rows() > 0)
			{
				$ssf_comp_array = array();
				foreach($ssf_items->result() as $row)
				{
					//$row->order
					$percent_value = $row->percent_value;
					$ssf_comp_array[] = ($gross_pay*($percent_value/100));
				}
				return $ssf_comp_array;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	//SSFs Percent and Value Pair
	function cal_ssf_percent_value_pair($ssf_cat,$gross_pay)
	{			
		//SSFs
		if(is_numeric($ssf_cat))
		{
			//Get ssf Components
			$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
			if($ssf_items->num_rows() > 0)
			{
				$ssf_comp_array = array();
				foreach($ssf_items->result() as $row)
				{
					//$row->order
					$percent_value = $row->percent_value;
					$ssf_comp_array[$percent_value] = ($gross_pay*($percent_value/100));
				}
				return $ssf_comp_array;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	//Default ssf info
	function default_ssf_percent_value($ssf_cat)
	{			
		if(is_numeric($ssf_cat))
		{
			//Net Basic Wage
			$ssf = $this->SSF_config->get_info($ssf_cat);
			$ssf_net_wage = $ssf->start; //Geting the dedault ssf value to cal the net wage
			$query_row = $this->SSF_config->get_default_ssf($ssf_net_wage);
			$percent_netwage = 0;
			$percent_value = 0;
			if($query_row)// Default exist 
			{
				$percent_value = $query_row->percent_value;
				return $percent_value;
			}
			else // Use next available value
			{
				$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
				if($ssf_items->num_rows() > 0)
				{
					foreach($ssf_items->result() as $row)
					{		
						$percent_value = $row->percent_value;
						break;						
					}
					return $percent_value;
				}
				else
				{
					return 0;
				}
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	//Getting the default SSF value
	function cal_default_ssf($ssf_cat,$gross_pay)
	{			
		if(is_numeric($ssf_cat))
		{
			//Net Basic Wage
			$ssf = $this->SSF_config->get_info($ssf_cat);
			$ssf_net_wage = $ssf->start; //Geting the dedault ssf value to cal the net wage
			$query_row = $this->SSF_config->get_default_ssf($ssf_net_wage);
			$percent_netwage = 0;
			$percent_value = 0;
			if($query_row)// Default exist 
			{
				$percent_value = $query_row->percent_value;
				$percent_netwage = ($gross_pay*($percent_value/100));
				return $percent_netwage;
			}
			else // Use next available value
			{
				$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
				if($ssf_items->num_rows() > 0)
				{
					foreach($ssf_items->result() as $row)
					{		
						$percent_value = $row->percent_value;
						$percent_netwage = ($gross_pay*($percent_value/100));
						break;						
					}
					return $percent_netwage;
				}
				else
				{
					return 0;
				}
			}
		}
		else
		{
			return 0;
		}
	}
	
	
	//Calc Net Basic Wage
	function cal_net_basic_wage($basic_wages,$default_ssf_value)
	{			
		//Net Basic Wage
		$net_basic_wage = $basic_wages - $default_ssf_value;
		
		return $net_basic_wage;
	}
	
	
	//Calc Gross Pay
	function cal_gross_pay($basic_wages,$total_allowances)
	{					
		return $basic_wages + $total_allowances;
	}
	
	//Tax
	function cal_tax($tax_cat,$taxable_income)
	{
		if(is_numeric($tax_cat))
		{
			$tax = $this->Tax_config->get_info($tax_cat);
			$exceeding = $tax->exceeding; 
		
			$tax_items = $this->Tax_config->get_tax_coponents($tax_cat);
			if($tax_items->num_rows() > 0)
			{
				if($taxable_income >= $tax_items->row()->amount)//Taxable income must be grater than the very first entry
				{
					$percent_tax = array();
					$percent_value = 0;
					foreach($tax_items->result() as $row)
					{
						$row_amount = $row->amount;
						if($row_amount <= $taxable_income)
						{
							$percent_value = $row->percent_value;
							$percent_tax[] = ($row_amount*($percent_value/100));
							$taxable_income = $taxable_income - $row_amount;
						}
						else
						{
							$percent_value = $row->percent_value;
							$percent_tax[] = ($taxable_income*($percent_value/100));
							$taxable_income = $taxable_income - $taxable_income;
							break;
						}
					}
					// Now Exceeding
					if($taxable_income > 0)
					{
						$percent_tax[] = ($taxable_income*($exceeding/100));
					}
		
					//Sum them all
					return number_format(array_sum($percent_tax), 2, '.', '');
				}
				else // No tax At all
				{
					return 0;
				}
			}
			else // Only Exceeding 
			{
				if($taxable_income > 0)
				{
					$only_exceeding = ($taxable_income*($exceeding/100));
				}
				return number_format($only_exceeding, 2, '.', '');
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	//Taxable Income
	function cal_taxable_income($gross_pay,$default_ssf_value)
	{					
		return $gross_pay - $default_ssf_value;
	}
	
	//Net Pay
	function cal_net_pay($taxable_income,$total_payroll_deductions)
	{					
		return $taxable_income - $total_payroll_deductions;
	}
	
	//total_payroll_deductions
	function cal_total_payroll_deductions($tax,$deduction_totals)
	{					
		return  $tax + $deduction_totals;
	}
	
	//Total SSF Payable
	function cal_total_ssf_payable($ssf_array)
	{					
		if(is_array($ssf_array))
		{
			return array_sum($ssf_array);
		}
		else
		{
			return $ssf_array;
		}
		
	}
	
	/////////////////////////////////*************************************////////////////////////////////////
	
	/*
	open a staff account
	*/
	function open_account($staff_id=-1)
	{
		$sortable_data = $this->get_account($staff_id);
		//staff info
		$staff_info = $this->Staff->get_info($staff_id);
			//$staff_info->first_name;
			
		$staff_name = $staff_info->last_name.' '.$staff_info->first_name;
		$staff_account_no = $staff_info->account_number;
		$staff_position = $staff_info->position;
		
		//Format in table
		$data['staff_id']=$staff_id;
		$data['staff_name']=$staff_name;
		$data['staff_account_no']=$staff_account_no;
		$data['staff_position']=$staff_position;
		$data['controller_name']=strtolower($this->uri->segment(1)); 
		$data['manage']= manage_payroll_table($sortable_data,$this);
		$data['form_width']=$this->get_form_width();
		$data['form_width2']=$this->get_form_width()+350;
			
		$this->load->view("staffs/staff_account",$data);
	}
	
	/*
	open a staff account
	*/
	function refresh_account($staff_id=-1)
	{
		$sortable_data = $this->get_account($staff_id);		
		$data_rows = manage_payroll_table_data_rows($sortable_data,$this);
		echo $data_rows;
	}
	
	/*
	New Pay Roll
	*/
	function new_payroll($staff_id=-1)
	{
		$data['person_id']= $staff_id;
		$data['controller_name']=strtolower($this->uri->segment(1));
		//taxs
		$taxs = array('no tax' => $this->lang->line('staffs_notax'));
		foreach($this->Tax_config->get_all()->result_array() as $row)
		{
			$taxs[$row['tax_id']] = $row['name'] ;
		}
		$data['taxs']=$taxs;
		//ssf
		$ssfs = array('no ssf' => $this->lang->line('staffs_nossf'));
		foreach($this->SSF_config->get_all()->result_array() as $row)
		{
			$ssfs[$row['ssf_id']] = $row['name'] ;
		}
		$data['ssfs']=$ssfs;
		
		$taxable = array(1=>'Yes',0=>'No');
		$data['taxable'] = $taxable;
		
		$this->load->view("staffs/payroll_form",$data);
	}
	
	/*
	New Pay Roll Annex
	*/
	function new_payroll_annex($staff_id=-1)
	{
		$data['person_id']= $staff_id;
		$data['controller_name']=strtolower($this->uri->segment(1));
		//taxs
		$taxs = array('no tax' => $this->lang->line('staffs_notax'));
		foreach($this->Tax_config->get_all()->result_array() as $row)
		{
			$taxs[$row['tax_id']] = $row['name'] ;
		}
		$data['taxs']=$taxs;
		//ssf
		$ssfs = array('no ssf' => $this->lang->line('staffs_nossf'));
		foreach($this->SSF_config->get_all()->result_array() as $row)
		{
			$ssfs[$row['ssf_id']] = $row['name'] ;
		}
		$data['ssfs']=$ssfs;
		
		$taxable = array(1=>'Yes',0=>'No');
		$data['taxable'] = $taxable;
		
		$this->load->view("staffs/payroll_form_annex",$data);
	}
	
	function save_payroll_annex($staff_id=-1)
	{
		$today_date = $this->input->post('payroll_date');
		if(empty($today_date))
		{
			$today_date = date('F d, Y');
		}
		
		$date_array = explode(',',$today_date);
		$_1 = explode(' ',$date_array[0]);
		$day = $_1[1];
		$month = $_1[0];
		$year = trim($date_array[1]);
		
		$item_data = array(
		'date'=>$today_date,
		'day'=>$day,
		'month'=>$month,
		'year'=>$year,
		'person_id'=>$staff_id,
		'basic_wages'=>$this->input->post('basic_wages'),
		'tax_cat'=>$this->input->post('tax_cat'),
		'ssf_cat'=>$this->input->post('ssf_cat')
		);
		///////////////////////////////////////////////////////////////////////////////////////////
		
		$allowance_array = $this->process_post_array_allowance($this->input->post('allowance_name'),$this->input->post('allowance_amount'));
		$deductions_array = $this->process_post_array($this->input->post('deductions_name'),$this->input->post('deductions_amount'));
			
		//save the order
		
		if($this->Staff->save_payroll($item_data))
		{	
			$payroll_id = $item_data['payroll_id'];
		
			$allowance_result = $this->saving_array_data_allowance($allowance_array,$payroll_id,'add allowance');
			$deductions_result = $this->saving_array_data($deductions_array,$payroll_id,'add deductions');
				
			if($allowance_result and $deductions_result)
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('items_successful_adding').' '.
				'','person_id'=>$staff_id));
			}
			else
			{
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding_updating').' '.
				'','person_id'=>-1));
			}
		}
		else//failure
		{	
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding_updating').' '.
			'','person_id'=>-1));
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////
	}
	
	
	function save_payroll($staff_id=-1)
	{
		$today_date = $this->input->post('payroll_date');
		if(empty($today_date))
		{
			$today_date = date('F d, Y');
		}
		
		$date_array = explode(',',$today_date);
		$_1 = explode(' ',$date_array[0]);
		$day = $_1[1];
		$month = $_1[0];
		$year = trim($date_array[1]);
		
		$item_data = array(
		'date'=>$today_date,
		'day'=>$day,
		'month'=>$month,
		'year'=>$year,
		'person_id'=>$staff_id,
		'basic_wages'=>$this->input->post('basic_wages'),
		'tax_cat'=>$this->input->post('tax_cat'),
		'ssf_cat'=>$this->input->post('ssf_cat')
		);
		///////////////////////////////////////////////////////////////////////////////////////////
		
		$allowance_array = $this->process_post_array_allowance($this->input->post('allowance_name'),$this->input->post('allowance_amount'));
		$deductions_array = $this->process_post_array($this->input->post('deductions_name'),$this->input->post('deductions_amount'));
			
		//save the order
		
		if($this->Staff->save_payroll($item_data))
		{	
			$payroll_id = $item_data['payroll_id'];
		
			$allowance_result = $this->saving_array_data_allowance($allowance_array,$payroll_id,'add allowance');
			$deductions_result = $this->saving_array_data($deductions_array,$payroll_id,'add deductions');
				
			if($allowance_result and $deductions_result)
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('items_successful_adding').' '.
				'','item_id'=>$payroll_id));
			}
			else
			{
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding_updating').' '.
				'','item_id'=>-1));
			}
		}
		else//failure
		{	
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding_updating').' '.
			'','item_id'=>-1));
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////
	}
	
	//Editing Payroll
	function edit_payroll($payroll_id=-1)
	{				
		$payroll_info = $this->Staff->get_payroll_for_editing($payroll_id);
		$payroll = $payroll_info['query_payroll']->row();
		$allowance = edit_allow_manage_table($payroll_info['query_allowance'],$this,'allowance');
		$deductions = edit_allow_diduc_manage_table($payroll_info['query_deductions'],$this,'deductions');
		//taxs
		$taxs = array('no tax' => $this->lang->line('staffs_notax'));
		foreach($this->Tax_config->get_all()->result_array() as $row)
		{
			$taxs[$row['tax_id']] = $row['name'] ;
		}
		$data['taxs']=$taxs;
		$data['selected_tax'] = $payroll->tax_cat;
		//ssf
		$ssfs = array('no ssf' => $this->lang->line('staffs_nossf'));
		foreach($this->SSF_config->get_all()->result_array() as $row)
		{
			$ssfs[$row['ssf_id']] = $row['name'] ;
		}
		$data['ssfs']=$ssfs;
		$data['selected_ssfs'] = $payroll->ssf_cat;
		
		$data['payroll']=$payroll;
		$data['manage_allowance']=$allowance;
		$data['manage_deductions']=$deductions;
		$data['controller_name']=strtolower($this->uri->segment(1));
				
		$this->load->view("staffs/edit_payroll", $data);
	}
	
	//Update Payroll 
	function update_payroll($payroll_id=-1)
	{
		$today_date = $this->input->post('payroll_date');
		if(empty($today_date))
		{
			$today_date = date('F d, Y');
		}
		
		$date_array = explode(',',$today_date);
		$_1 = explode(' ',$date_array[0]);
		$day = $_1[1];
		$month = $_1[0];
		$year = trim($date_array[1]);
		
		$staff_id = $this->input->post('hidden_person');
		
		$item_data = array(
		'date'=>$today_date,
		'day'=>$day,
		'month'=>$month,
		'year'=>$year,
		'person_id'=>$staff_id,
		'basic_wages'=>$this->input->post('basic_wages'),
		'tax_cat'=>$this->input->post('tax_cat'),
		'ssf_cat'=>$this->input->post('ssf_cat')
		);
		
		///////////////////////////////////////////////////////////////////////////////////////////
		$hidden_allowance = $this->input->post('hidden_allowance');
		$hidden_deductions = $this->input->post('hidden_deductions');
		
		if($hidden_allowance == 1)
		{
			$allowance_array = $this->update_post_array_allowance($this->input->post('allowance_name'),$this->input->post('allowance_amount'),$this->input->post('allowance_update_id'));
		}
		
		if($hidden_deductions == 1)
		{
			$deductions_array = $this->update_post_array($this->input->post('deductions_name'),$this->input->post('deductions_amount'),$this->input->post('deductions_update_id'));
		}
		
		//save the order
		
		if($this->Staff->update_payroll($item_data,$payroll_id))
		{	
			if($hidden_allowance == 1)
			{
				$allowance_result = $this->update_array_data_allowance($allowance_array,$payroll_id,'update allowance');
			}
			
			if($hidden_deductions == 1)
			{
				$deductions_result = $this->update_array_data($deductions_array,$payroll_id,'update deductions');
			}
				
			//if($allowance_result and $deductions_result)
			//{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('common_successful_update').' '.
				'','item_id'=>$payroll_id));
			//}
			//else
			//{
				//echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding_updating').' '.
				//'','item_id'=>-1));
			//}
		}
		else//failure
		{	
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding_updating').' '.
			'','item_id'=>-1));
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////
	}
	
	function process_post_array($post_name,$post_amount)
	{
		$name = array();
		$amount = array();
		$final = array();
			
		foreach($post_name as $datax)
		{
			$name[] = $datax;
		}
		
		foreach($post_amount as $datax)
		{
			$amount[] = $datax;
		}
			
		//Put them in One array as key and value
		for($x=0; $x < count($name); $x++)
		{
			$final[$name[$x]] = $amount[$x];	
		}
		
		return $final;
	}
	
	function process_post_array_allowance($post_name,$post_amount)
	{
		$name = array();
		$amount = array();
		$taxable = array();
		$final = array();
			
		foreach($post_name as $datax)
		{
			$name[] = $datax;
		}
		
		foreach($post_amount as $datax)
		{
			$amount[] = $datax;
		}
		$cunt = 1;
		foreach($post_name as $datax)
		{
			$chk = $this->input->post('taxable'.$cunt);
			$taxable[] = $chk =='' ? 0:$chk;
			
			$cunt++;
		}
		
		//Merge the amount and taxable
		for($x=0; $x < count($amount); $x++)
		{
			$pair_array = array();
			$pair_array[0] = $amount[$x];
			$pair_array[1] = $taxable[$x];
			
			$final_pair_array[] = $pair_array;	
		}
			
		//Put them in One array as key and value
		for($x=0; $x < count($name); $x++)
		{
			$final[$name[$x]] = $final_pair_array[$x];	
		}
		
		return $final;
	}
	
	function saving_array_data_allowance($item_data,$item_id,$_to_save)
	{
		$number_of_processed = 0;
		foreach($item_data as $key => $value)
		{
		  $amount = $value[0];//
		  $taxable = $value[1];//
		  
		  $compo_data = array(
		  'payroll_id'=>$item_id,
		  'name'=>$key,
		  'amount'=>$amount,
		  'taxable'=>$taxable
		  );
		  
		  if($_to_save == 'add allowance')
		  {
			$db_result = $this->Staff->save_payroll_allowance($compo_data);
		  }
		  else
		  {
			$db_result = $this->Staff->save_payroll_deductions($compo_data);
		  }
		  
		  if($db_result)
		  {
			  $number_of_processed++;
		  }
		}
				
		if($number_of_processed == count($item_data))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	
	function saving_array_data($item_data,$item_id,$_to_save)
	{
		$number_of_processed = 0;
		foreach($item_data as $key => $value)
		{			  
		  $compo_data = array(
		  'payroll_id'=>$item_id,
		  'name'=>$key,
		  'amount'=>$value
		  );
		  
		  if($_to_save == 'add allowance')
		  {
			$db_result = $this->Staff->save_payroll_allowance($compo_data);
		  }
		  else
		  {
			$db_result = $this->Staff->save_payroll_deductions($compo_data);
		  }
		  
		  if($db_result)
		  {
			  $number_of_processed++;
		  }
		}
				
		if($number_of_processed == count($item_data))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
	function update_post_array_allowance($post_name,$post_amount,$post_upt_id)
	{
		$name = array();
		$amount = array();
		$taxable = array();
		$update_id = array();
		$final_pair_array = array();
		$final = array();
			
		foreach($post_name as $datax)
		{
			$name[] = $datax;
		}
		
		foreach($post_amount as $datax)
		{
			$amount[] = $datax;
		}
		
		$cunt = 1;
		foreach($post_name as $datax)
		{
			$chk = $this->input->post('taxable'.$cunt);
			$taxable[] = $chk =='' ? 0:$chk;
			
			$cunt++;
		}
		
		foreach($post_upt_id as $datax)
		{
			$update_id[] = $datax;
		}
		
		
		//Merge the amount and name
		for($x=0; $x < count($name); $x++)
		{
			$pair_array = array();
			$pair_array[0] = $name[$x];
			$pair_array[1] = $amount[$x];
			$pair_array[2] = $taxable[$x];
			
			$final_pair_array[] = $pair_array;	
		}
			
		//Put them in One array as key and value
		for($x=0; $x < count($update_id); $x++)
		{
			$final[$update_id[$x]] = $final_pair_array[$x];	
		}
		
		return $final;
	}
	
	function update_array_data_allowance($item_data,$item_id,$_to_save)
	{
		$number_of_processed = 0;
		foreach($item_data as $key => $value)
		{
		  $name = $value[0];//
		  $amount = $value[1];//
		  $taxable = $value[2];//
			
		  $compo_data = array(
		  'payroll_id'=>$item_id,
		  'name'=>$name,
		  'amount'=>$amount,
		  'taxable'=>$taxable
		  );
		  
		  if($_to_save == 'update allowance')
		  {
			$db_result = $this->Staff->update_payroll_allowance($compo_data,$key);
		  }
		  else
		  {
			$db_result = $this->Staff->update_payroll_deductions($compo_data,$key);
		  }
		  
		  if($db_result)
		  {
			  $number_of_processed++;
		  }
		}
				
		if($number_of_processed == count($item_data))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
	
	function update_post_array($post_name,$post_amount,$post_upt_id)
	{
		$name = array();
		$amount = array();
		$update_id = array();
		$final_pair_array = array();
		$final = array();
			
		foreach($post_name as $datax)
		{
			$name[] = $datax;
		}
		
		foreach($post_amount as $datax)
		{
			$amount[] = $datax;
		}
		
		foreach($post_upt_id as $datax)
		{
			$update_id[] = $datax;
		}
		
		//Merge the amount and name
		for($x=0; $x < count($name); $x++)
		{
			$pair_array = array();
			$pair_array[0] = $name[$x];
			$pair_array[1] = $amount[$x];
			
			$final_pair_array[] = $pair_array;	
		}
			
		//Put them in One array as key and value
		for($x=0; $x < count($update_id); $x++)
		{
			$final[$update_id[$x]] = $final_pair_array[$x];	
		}
		
		return $final;
	}
	
	function update_array_data($item_data,$item_id,$_to_save)
	{
		$number_of_processed = 0;
		foreach($item_data as $key => $value)
		{
		  $name = $value[0];//
		  $amount = $value[1];//
			
		  $compo_data = array(
		  'payroll_id'=>$item_id,
		  'name'=>$name,
		  'amount'=>$amount
		  );
		  
		  if($_to_save == 'update allowance')
		  {
			$db_result = $this->Staff->update_payroll_allowance($compo_data,$key);
		  }
		  else
		  {
			$db_result = $this->Staff->update_payroll_deductions($compo_data,$key);
		  }
		  
		  if($db_result)
		  {
			  $number_of_processed++;
		  }
		}
				
		if($number_of_processed == count($item_data))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	///////////// delete orders //////////////////////
	function delete_payroll()
	{
		$items_to_delete = $this->input->post('ids');
		
		if($this->Staff->delete_payroll($items_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('items_successful_deleted').' '.
			count($items_to_delete).' '.$this->lang->line('items_one_or_multiple')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_cannot_be_deleted')));
		}
	}
	
	function delete_allow_diduc()
	{
		$row_id=$this->input->post('row_id');
		$payroll_id=$this->input->post('payroll_id');
		$who=$this->input->post('who');
		
		if($who == 1)
		{
			if($this->Staff->delete_allowance($row_id))
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('common_deleted').' '.
				'','item_id'=>$payroll_id));
			}
			else
			{
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_cannot_be_deleted')));
			}
		}
		else
		{
			if($this->Staff->delete_deductions($row_id))
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('common_deleted').' '.
				'','item_id'=>$payroll_id));
			}
			else
			{
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_cannot_be_deleted')));
			}
		}
		
	}	

	
	////Stament of Account////////////////
	function statement_of_account($staff_id)
	{
		$sortable_data = $this->get_account($staff_id);
		$staff_info = $this->Staff->get_info($staff_id);
			//$staff_info->first_name;
			
		$staff_name = $staff_info->first_name;
			if(empty($staff_name))
			{
				$staff_name = '[None]';
			}
		
		//Format in table
		$data['staff_id']=$staff_id;
		$data['staff_name']=$staff_name;   	 	 	 
		$data['phone_number']=$staff_info->phone_number;
		$data['email']=$staff_info->email;
		$data['address_1']=$staff_info->address_1;
		$data['address_2']=$staff_info->address_2;
		$data['account_number']=$staff_info->account_number;		
		$data['controller_name']=strtolower($this->uri->segment(1));
		$data['form_width']=$this->get_form_width();
		$data['manage']=statement_acct_order_table($sortable_data,$this);
		$this->load->view("staffs/print_sate_acct",$data);
		
		return;
	}
	
}
?>